package logic

import (
	"context"
	"fmt"
	"wx/models/cache"
	"wx/models/mysql"

	"wx/wxrpc/internal/svc"
	"wx/wxrpc/wxrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *wxrpc.RegisterRequest) (*wxrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	get := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("手机号烟瘴吗错误")
	}
	var wexx mysql.Ass
	mysql.DB.Where("mobile", in.Mobile).Find(&wexx)
	if wexx.ID != 0 {
		return nil, fmt.Errorf("当前账号已存在请前去登陆")
	}
	ass := mysql.Ass{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Create(&ass)
	return &wxrpc.RegisterResponse{
		UserId: int64(ass.ID),
	}, nil
}
